package com.zhao.shiyuanbaoyou.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhao.shiyuanbaoyou.R;
import com.zhao.shiyuanbaoyou.bean.BrandBean;
import com.zhao.shiyuanbaoyou.bean.BrandcBean;
import com.zhao.shiyuanbaoyou.helper.RecyclerViewAdapterHelper;
import com.zhao.shiyuanbaoyou.urlutil.Constant;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张昭 on 2016/11/12.
 */

public class BrandcAdapter extends RecyclerViewAdapterHelper<BrandBean.RowsBean> {
    private Context mContext;
    private List<BrandBean.RowsBean> mList;
    private List<BrandcBean.HandBean> mList_img;
     public BrandcAdapter(Context context, List<BrandBean.RowsBean> list) {
        super(context, list);
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_item_fragment_brand, parent, false);
        return new MyBrandViewHolder(view);
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        //保留小数点后两位

        DecimalFormat df = new DecimalFormat("0.0#");
        if (holder instanceof MyBrandViewHolder) {
            ((MyBrandViewHolder) holder).textView_name.setText(mList.get(position).getName());
            ((MyBrandViewHolder) holder).button_zhekou.setText(df.format(mList.get(position).getDisCount()) + "折起");
            ((MyBrandViewHolder) holder).textView_day.setText("剩余8天");
            Glide.with(mContext)
                    .load(Constant.BASE_URL + mList.get(position).getImgUrlSml())
                    .into(((MyBrandViewHolder) holder).imageView_name);
            String path="{"+"hand"+":"+mList.get(position).getProductInfo()+"}";
           //Log.i("TAG", "==================onBindMyViewHolder: "+path);
            BrandcBean brandcBean = parseJsonToBean(path);
            mList_img=brandcBean.getHand();
            Glide.with(mContext).load(mList_img.get(0).getProductImg()).into(((MyBrandViewHolder) holder).imageView_left);
            Glide.with(mContext).load(mList_img.get(1).getProductImg()).into(((MyBrandViewHolder) holder).imageView_righttop);
            Glide.with(mContext).load(mList_img.get(2).getProductImg()).into(((MyBrandViewHolder) holder).imageView_rightxia);
        }

    }

    static class MyBrandViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView_name, imageView_left, imageView_righttop, imageView_rightxia;
        private TextView textView_name, textView_day;
        private Button button_zhekou;

        public MyBrandViewHolder(View itemView) {
            super(itemView);
            imageView_name = (ImageView) itemView.findViewById(R.id.imageView_fragment_brand);
            imageView_left = (ImageView) itemView.findViewById(R.id.imageView_fragment_brand_left);
            imageView_righttop = (ImageView) itemView.findViewById(R.id.imageView_fragment_brand_Righttop);
            imageView_rightxia = (ImageView) itemView.findViewById(R.id.imageView_fragment_brand_Rightxia);
            textView_day = (TextView) itemView.findViewById(R.id.textView_fragment_brand_day);
            textView_name = (TextView) itemView.findViewById(R.id.textView_fragment_name_brand);
            button_zhekou = (Button) itemView.findViewById(R.id.buttonn_fragment_brand);

        }
    }

    //解析方法
    public BrandcBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<BrandcBean>() {
        }.getType());
    }
}
